$("#key-query").on('click', function () {
    var param = {startIndex: 0, pageSize: 3, name: $('#majorType').val().trim()};   //分业查询
    var $table = $('#table-list');
    var $wrapper = $('#div-table-container');//暂时不用
    var ajaxUrl = '/student/major/getPage';
    console.info(param);
    $table.dataTable().fnDestroy();
    var _table = $table.dataTable($.extend(true, {}, CONSTANT.DATA_TABLES.DEFAULT_OPTION, {
        ajax: function (data, callback, settings) {
            param.startIndex = data.start;
            $.ajax({
                type: "post",
                url: ajaxUrl,
                cache: false,
                async: false,
                data: param,        //传入请求参数
                contentType: "application/x-www-form-urlencoded; charset=UTF-8",
                dataType: "json",
                success: function (result) {
                    setTimeout(function () {
                        var returnData = {};
                        returnData.draw = data.draw + 1;
                        returnData.recordsTotal = result.total;
                        returnData.recordsFiltered = result.total;//每次翻页过滤数据条数
                        returnData.data = result.pageData;
                        // $wrapper.spinModal(false);
                        callback(returnData);
                    }, 200);
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert("查询失败");
                    // $wrapper.spinModal(false);
                }
            });
        },
        ordering: false,
        pageLength: param.pageSize,// 每页条数 设置
        columns: [
            CONSTANT.DATA_TABLES.COLUMN.CHECKBOX,
            {
                className: "ellipsis",
                data: "id",
                width: "40px",
                render: CONSTANT.DATA_TABLES.RENDER.ELLIPSIS,
            },
            {
                className: "ellipsis",
                data: "majorName",
                render: CONSTANT.DATA_TABLES.RENDER.ELLIPSIS
            },
            {
                className: "ellipsis",
                data: "majorCount",
                width: "100px",
                render: CONSTANT.DATA_TABLES.RENDER.ELLIPSIS

            },
            {
                className: "ellipsis",
                data: "majorYear",
                width: "60px",
                render: CONSTANT.DATA_TABLES.RENDER.ELLIPSIS
            },
            {
                className: "ellipsis",
                data: "majorScore",
                render: CONSTANT.DATA_TABLES.RENDER.ELLIPSIS
            },
            {
                className: "td-operation",
                data: null,
                defaultContent: "",
                width: "200px"
            }
        ],
        "createdRow": function (row, data, index) {    //创建列
            if (data.role) {
                $(row).addClass("info");
            }
            var $btnDetail = $('<button type="button" class="btn btn-default btn-ssm btn-show">详细</button>');
            $('td', row).eq(6).append($btnDetail);

            var $btnDetail = $('<button type="button" class="btn btn-default btn-ssm btn-edit">修改</button>');
            $('td', row).eq(6).append($btnDetail);

            var $btnDetail = $('<button type="button" class="btn btn-default btn-ssm btn-delete">删除</button>');
            $('td', row).eq(6).append($btnDetail);
        },
        "drawCallback": function (settings) {
            $(":checkbox[name='cb-check-all']", $wrapper).prop('checked', false);
            $("tbody tr", $table).eq(0).click();
        }
    })).api();

    $table.on('change', ':checkbox', function () {
        if ($(this).is("[name='cb-check-all']")) {
            $(":checkbox", $table).prop("checked", $(this).prop("checked"));
        } else {
            var checkbox = $("tbody :checkbox", $table);
            $(":checkbox[name='cb-check-all']", $table).prop('checked', checkbox.length == checkbox.filter(':checked').length);//全选中自动勾选全选
        }
    }).on("click", ".td-checkbox", function (event) {
        !$(event.target).is(":checkbox") && $(":checkbox", this).trigger("click");
    }).on("click", ".btn-edit", function () {
        // 点击修改按钮
        var item = _table.row($(this).closest('tr')).data();//提取tr中的数据值（json）
        $('#edit-major-id').val(item.id);
        $('#edit-major-name').val(item.majorName);
        $('#edit-major-count').val(item.majorCount);
        $('#edit-major-year').val(item.majorYear);
        $('#edit-major-score').val(item.majorScore);
        $('#majorEdit').modal('show');
    }).on("click", ".btn-delete", function () {
        // 点击删除按钮
        var item = _table.row($(this).closest('tr')).data();
        var flag = confirm("确定删除专业记录吗？");
        console.info(flag);
        var objP = {'id': item.id};
        console.info(objP);
        if (flag) {
            $.ajax({
                type: "post",
                url: "/student/major/deleteMajor",
                cache: false,
                data: objP,
                contentType: "application/x-www-form-urlencoded; charset=UTF-8",
                dataType: "json",
                success: function (result) {
                    console.info(result);
                    if (result.success) {
                        _table.draw(false);
                    } else {
                        alert(result.message);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert("保存失败");
                }
            });
        }
    }).on('click', '.btn-show', function () {
        var item = _table.row($(this).closest('tr')).data();
        $('#show-major-id').val(item.id);
        $('#show-major-name').val(item.majorName);
        $('#show-major-count').val(item.majorCount);
        $('#show-major-year').val(item.majorYear);
        $('#show-major-score').val(item.majorScore);
        $('#majorShow').modal('show');

    })


    $('#btn-add-Major').on('click', function () {
        var major = {};
        major.majorName = $('#add-major-name').val().trim();
        major.majorCount = $('#add-major-count').val().trim();
        major.majorYear = $('#add-major-year').val().trim();
        major.majorScore = $('#add-major-score').val().trim();
        console.info(major);
        if (major.name == "") {
            alert("专业名称不能为空！！！");
            return;
        }
        if (major.coount <= 0) {
            alert("专业人数不符合条件！！！");
            return;
        }
        $.ajax({
            type: 'post',
            url: '/student/major/addMajor',
            data: major,
            cache: false,
            dataType: "json",
            contentType: "application/x-www-form-urlencoded; charset=UTF-8",
            success: function (result) {
                if (result.success) {
                    _table.draw(false);
                    $('#majorAdd').modal("hide");
                    document.getElementById('form-add-major').reset();//重置表格
                } else {
                    alert(result.message);//类型不一致
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert("保存失败");
            }
        })
    })


    $('#btn-edit-Major').on('click', function () {
        var major = {};
        major.id = $('#edit-major-id').val().trim();
        major.majorName = $('#edit-major-name').val().trim();
        major.majorCount = $('#edit-major-count').val().trim();
        major.majorYear = $('#edit-major-year').val().trim();
        major.majorScore = $('#edit-major-score').val().trim();
        console.info(major);
        if (major.name == "") {
            alert("专业名称不能为空！！！");
            return;
        }
        if (major.coount <= 0) {
            alert("专业人数不符合条件！！！");
            return;
        }
        $.ajax({
            type: 'post',
            url: '/student/major/updateMajor',
            data: major,
            cache: false,
            dataType: "json",
            contentType: "application/x-www-form-urlencoded; charset=UTF-8",
            success: function (result) {
                if (result.success) {
                    _table.draw(false);
                    $('#majorEdit').modal("hide");
                    document.getElementById('form-edit-major').reset();//重置表格
                } else {
                    alert(result.message);//类型不一致
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert("保存失败");
            }
        })
    })
});
$('.table tr').on('click', function () {
    var self = $(this);
    $('#left-menu tr').removeClass('success');
    self.addClass('success');
})

